﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// add
using System.Xml.Linq;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
//using System.Collections.ObjectModel;

namespace FormBase
{
// sample xml file:
//<?xml version="1.0" encoding="utf-8"?>
//<configuration>
//  <ProjectID>MIR3</ProjectID>
//  <CompanyTitle>Champtek</CompanyTitle>
//  <Language>en</Language>
//  <ConnectionType>RS232</ConnectionType>
//  <RS232_PortName>COM1</RS232_PortName>
//  <RS232_BaudRate>38400</RS232_BaudRate>
//  <RS232_StopBits>One</RS232_StopBits>
//  <RS232_DataBits>8</RS232_DataBits>
//  <RS232_Parity>None</RS232_Parity>
//  <RS232_Handshake>None</RS232_Handshake>
//</configuration>
    public class CPropertyXML
    {
        XDocument mDoc = null;
        XElement mElementRoot;
        string msMsg;
        string msFilePath;
        string msRootNode;
        string msRootNodeIDAttribute;
        Dictionary<string, string> mDict = new Dictionary<string, string>();

        public string Message { get { return msMsg; } }
        public string FilePath { get { return msFilePath; } }

        /// <summary>
        /// Example: 
        /// /Root/Group
        /// /Root/Company/System/Project
        /// </summary>
        public string RootNode { get { return msRootNode; } }
        public string RootNoteIDAttribute { get { return msRootNodeIDAttribute; } }

        public CPropertyXML()
        {
        }
        public bool OpenFile(string sFilePath, Type enumType)
        {
            try
            {
                msMsg = string.Empty;
                foreach (string sKey in Enum.GetNames(enumType))
                    mDict.Add(sKey, string.Empty);

                if (System.IO.File.Exists(sFilePath))
                {
                    mDoc = XDocument.Load(sFilePath);
                }
                else
                {

//                    XElement contacts =
//    new XElement("Contacts",
//        new XElement("Contact",
//            new XElement("Name", "Patrick Hines"),                                                 
//            new XElement("Phone", "206-555-0144"),
//            new XElement("Address",
//                new XElement("Street1", "123 Main St"),
//                new XElement("City", "Mercer Island"),
//                new XElement("State", "WA"),
//                new XElement("Postal", "68042")
//            )
//        )
//    );
//Console.WriteLine(contacts);

                    XNamespace ns = CProject.mApp.msNamespace;
                    mDoc = new XDocument();

                    // todo
                    XElement eProject = new XElement("Company", new XAttribute("ID", CProject.mApp.msCompanyID)
                        , new XElement("System", new XAttribute("ID", CProject.mApp.msSystemID)
                            , new XElement("Project", new XAttribute("ID", CProject.mApp.msProjectID))));

                    //mDoc.Root.Add(eProject);
                    foreach (KeyValuePair<string, string> kvp in mDict)
                    {
                        //eProject.Add(kvp.Key, kvp.Value);
                        //e1 = mDoc.Root.Element(kvp.Key);
                        //if (e1 == null)
                        //    sValue = string.Empty;
                        //else
                        //    sValue = e1.Value;

                        //mDict[kvp.Key] = sValue;
                    }

                    //XElement e1 = mDoc.Root;
                    //XElement eRoot = new XElement(ns + "Root"
                    //    ,new XElement("Company", new XAttribute("ID", CProject.CompanyID)));

                    //mDoc.Add(eRoot);
                    ////mDoc.Add(new XElement("configuration"));
                    ////string sQuery = string.Format("{0}[@ID='{1}']", msRootNode, msRootNodeIDAttribute);
                    //XElement eDict;
                    //foreach (KeyValuePair<string, string> kvp in mDict)
                    //{
                    //    e1 = mDoc.Root.Element(kvp.Key);
                    //    if (e1 == null)
                    //        sValue = string.Empty;
                    //    else
                    //        sValue = e1.Value;

                    //    mDict[kvp.Key] = sValue;
                    //}

                    //XElement eTree1 = new XElement(ns + sQuery, 
                    //    new XElement(CProject.CompanyID), 
                    //    new XElement(CProject.SystemID),
                    //    new XElement(CProject.ProjectID),
                    //mElementRoot = new XElement("
                    //mDoc.Add(new XElement();
                }
                return true;
            }
            catch (Exception e1)
            {
                msMsg = e1.Message;
                return false;
            }
        }
        public string GetValue(string sKey)
        {
            if (mDict.ContainsKey(sKey))
            {
                msMsg = string.Empty;
                return mDict[sKey];
            }
            else
            {
                msMsg = "Not found";
                return string.Empty;
            }
        }
        public bool LoadConfiguration()
        {
            try
            {
                XElement e1;
                string sValue;
                foreach (KeyValuePair<string, string> kvp in mDict)
                {
                    e1 = mDoc.Root.Element(kvp.Key);
                    if (e1 == null)
                        sValue = string.Empty;
                    else
                        sValue = e1.Value;

                    mDict[kvp.Key] = sValue;
                }
                return true;
            }
            catch (Exception e1)
            {
                msMsg = e1.Message;
                return false;
            }
        }
        public bool SaveConfiguration()
        {
            try
            {
                XElement e1;
                foreach (KeyValuePair<string, string> kvp in mDict)
                {
                    e1 = mDoc.Root.Element(kvp.Key);
                    if (e1 == null)
                        mDoc.Root.Add(new XElement(kvp.Key, kvp.Value));
                    else
                        e1.Value = kvp.Value;
                }
                return true;
            }
            catch (Exception e1)
            {
                msMsg = e1.Message;
                return false;
            }
        }
    }
}
